namespace DotNetNuke.Entities.Modules
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Data;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Entities.Tabs;
    using DotNetNuke.Security;
    using DotNetNuke.Security.Permissions;
    using DotNetNuke.Services.Exceptions;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Data;
    using System.Runtime.CompilerServices;

    public class ModuleController
    {
        public int AddModule(ModuleInfo objModule)
        {
            if (Null.IsNull(objModule.ModuleID))
            {
                objModule.ModuleID = DataProvider.Instance().AddModule(objModule.PortalID, objModule.ModuleDefID, objModule.ModuleTitle, objModule.AllTabs, objModule.Header, objModule.Footer, objModule.StartDate, objModule.EndDate, objModule.InheritViewPermissions, objModule.IsDeleted);
                if (objModule.ModulePermissions != null)
                {
                    IEnumerator refObjectHelperL0=null;
                    ModulePermissionController objModulePermissionController = new ModulePermissionController();
                    ModulePermissionCollection objModulePermissions = objModule.ModulePermissions;
                    ModulePermissionInfo objModulePermission = new ModulePermissionInfo();
                    try
                    {
                        refObjectHelperL0 = objModulePermissions.GetEnumerator();
                        while (refObjectHelperL0.MoveNext())
                        {
                            objModulePermission = (ModulePermissionInfo) refObjectHelperL0.Current;
                            objModulePermission.ModuleID = objModule.ModuleID;
                            objModulePermissionController.AddModulePermission(objModulePermission);
                        }
                    }
                    finally
                    {
                        if (refObjectHelperL0 is IDisposable)
                        {
                            (refObjectHelperL0 as IDisposable).Dispose();
                        }
                    }
                }
            }
            try
            {
                DataProvider.Instance().AddTabModule(objModule.TabID, objModule.ModuleID, objModule.ModuleOrder, objModule.PaneName, objModule.CacheTime, objModule.Alignment, objModule.Color, objModule.Border, objModule.IconFile, (int) objModule.Visibility, objModule.ContainerSrc, objModule.DisplayTitle, objModule.DisplayPrint, objModule.DisplaySyndicate);
                if (objModule.ModuleOrder == -1)
                {
                    this.UpdateModuleOrder(objModule.TabID, objModule.ModuleID, objModule.ModuleOrder, objModule.PaneName);
                }
                else
                {
                    this.UpdateTabModuleOrder(objModule.TabID, objModule.PortalID);
                }
            }
            catch (Exception exception1)
            {
                
                
            }
            return objModule.ModuleID;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// CopyModule copies a Module from one Tab to a collection of Tabs optionally
        /// including the TabModule settings
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="moduleId">The Id of the module to copy</param>
        /// <param name="fromTabId">The Id of the source tab</param>
        /// <param name="toTabs">An ArrayList of TabItem objects</param>
        /// <param name="includeSettings">A flag to indicate whether the settings are copied to the new Tab</param>
        /// <history>
        /// [cnurse]	10/22/2004	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public void CopyModule(int moduleId, int fromTabId, ArrayList toTabs, bool includeSettings)
        {
            int refIntHelperL0 = toTabs.Count - 1;
            for (int intTab = 0; intTab <= refIntHelperL0; intTab++)
            {
                TabInfo objTab = (TabInfo) toTabs[intTab];
                if (objTab.TabID != fromTabId)
                {
                    this.CopyModule(moduleId, fromTabId, objTab.TabID, "", includeSettings);
                }
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// CopyModule copies a Module from one Tab to another optionally including all the 
        /// TabModule settings
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="moduleId">The Id of the module to copy</param>
        /// <param name="fromTabId">The Id of the source tab</param>
        /// <param name="toTabId">The Id of the destination tab</param>
        /// <param name="toPaneName">The name of the Pane on the destination tab where the module will end up</param>
        /// <param name="includeSettings">A flag to indicate whether the settings are copied to the new Tab</param>
        /// <history>
        /// [cnurse]	10/21/2004	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public void CopyModule(int moduleId, int fromTabId, int toTabId, string toPaneName, bool includeSettings)
        {
            ModuleInfo objModule = this.GetModule(moduleId, fromTabId);
            if (toPaneName == "")
            {
                toPaneName = objModule.PaneName;
            }
            try
            {
                DataProvider.Instance().AddTabModule(toTabId, moduleId, -1, toPaneName, objModule.CacheTime, objModule.Alignment, objModule.Color, objModule.Border, objModule.IconFile, (int) objModule.Visibility, objModule.ContainerSrc, objModule.DisplayTitle, objModule.DisplayPrint, objModule.DisplaySyndicate);
                if (includeSettings)
                {
                    ModuleInfo toModule = this.GetModule(moduleId, toTabId);
                    this.CopyTabModuleSettings(objModule, toModule);
                }
            }
            catch (Exception exception1)
            {
                
                
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// CopyTabModuleSettings copies the TabModuleSettings from one instance to another
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="fromModule">The module to copy from</param>
        /// <param name="toModule">The module to copy to</param>
        /// <history>
        /// [cnurse]	01/11/2005	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public void CopyTabModuleSettings(ModuleInfo fromModule, ModuleInfo toModule)
        {
            IDictionaryEnumerator refObjectHelperL0 = this.GetTabModuleSettings(fromModule.TabModuleID).GetEnumerator();
            while (refObjectHelperL0.MoveNext())
            {
                DictionaryEntry setting = (DictionaryEntry) refObjectHelperL0.Current;
                this.UpdateTabModuleSetting(toModule.TabModuleID, Conversions.ToString(setting.Key), Conversions.ToString(setting.Value));
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// DeleteAllModules deletes all instaces of a Module (from a collection), optionally excluding the
        /// current instance, and optionally including deleting the Module itself.
        /// </summary>
        /// <remarks>
        /// Note - the base module is not removed unless both the flags are set, indicating
        /// to delete all instances AND to delete the Base Module
        /// </remarks>
        /// <param name="moduleId">The Id of the module to copy</param>
        /// <param name="tabId">The Id of the current tab</param>
        /// <param name="fromTabs">An ArrayList of TabItem objects</param>
        /// <param name="includeCurrent">A flag to indicate whether to delete from the current tab
        /// as identified ny tabId</param>
        /// <param name="deleteBaseModule">A flag to indicate whether to delete the Module itself</param>
        /// <history>
        /// [cnurse]	10/22/2004	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public void DeleteAllModules(int moduleId, int tabId, ArrayList fromTabs, bool includeCurrent, bool deleteBaseModule)
        {
            int refIntHelperL0 = fromTabs.Count - 1;
            for (int intTab = 0; intTab <= refIntHelperL0; intTab++)
            {
                TabInfo objTab = (TabInfo) fromTabs[intTab];
                if ((objTab.TabID != tabId) | includeCurrent)
                {
                    this.DeleteTabModule(objTab.TabID, moduleId);
                }
            }
            if (includeCurrent & deleteBaseModule)
            {
                this.DeleteModule(moduleId);
            }
        }

        public void DeleteModule(int ModuleId)
        {
            DataProvider.Instance().DeleteModule(ModuleId);
            DataProvider.Instance().DeleteSearchItems(ModuleId);
        }

        public void DeleteModuleSetting(int ModuleId, string SettingName)
        {
            DataProvider.Instance().DeleteModuleSetting(ModuleId, SettingName);
        }

        public void DeleteModuleSettings(int ModuleId)
        {
            DataProvider.Instance().DeleteModuleSettings(ModuleId);
        }

        public void DeleteTabModule(int TabId, int ModuleId)
        {
            ModuleInfo objModule = this.GetModule(ModuleId, TabId);
            DataProvider.Instance().DeleteTabModule(TabId, ModuleId);
            this.UpdateTabModuleOrder(TabId, objModule.PortalID);
            if (this.GetModule(ModuleId, Null.NullInteger).TabID == Null.NullInteger)
            {
                objModule.TabID = Null.NullInteger;
                objModule.IsDeleted = true;
                this.UpdateModule(objModule);
                DataProvider.Instance().DeleteSearchItems(ModuleId);
            }
        }

        public void DeleteTabModuleSetting(int TabModuleId, string SettingName)
        {
            DataProvider.Instance().DeleteTabModuleSetting(TabModuleId, SettingName);
        }

        public void DeleteTabModuleSettings(int TabModuleId)
        {
            DataProvider.Instance().DeleteTabModuleSettings(TabModuleId);
        }

        private ModuleInfo FillModuleInfo(IDataReader dr)
        {
            return this.FillModuleInfo(dr, true, true);
        }

        private ModuleInfo FillModuleInfo(IDataReader dr, bool CheckForOpenDataReader)
        {
            return this.FillModuleInfo(dr, CheckForOpenDataReader, true);
        }

        private ModuleInfo FillModuleInfo(IDataReader dr, bool CheckForOpenDataReader, bool IncludePermissions)
        {
            ModuleInfo objModuleInfo = new ModuleInfo();
            ModulePermissionController objModulePermissionController = new ModulePermissionController();
            bool Continue = true;
            if (CheckForOpenDataReader)
            {
                Continue = false;
                if (dr.Read())
                {
                    Continue = true;
                }
            }
            if (!Continue)
            {
                return null;
            }
            try
            {
                objModuleInfo.PortalID = Convert.ToInt32(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["PortalID"]), objModuleInfo.PortalID)));
            }
            catch (Exception exception1)
            {
                
                
            }
            try
            {
                objModuleInfo.TabID = Convert.ToInt32(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["TabID"]), objModuleInfo.TabID)));
            }
            catch (Exception exception2)
            {
                ProjectData.SetProjectError(exception2);
                
            }
            try
            {
                objModuleInfo.TabModuleID = Convert.ToInt32(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["TabModuleID"]), objModuleInfo.TabModuleID)));
            }
            catch (Exception exception3)
            {
                ProjectData.SetProjectError(exception3);
                
            }
            try
            {
                objModuleInfo.ModuleID = Convert.ToInt32(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["ModuleID"]), objModuleInfo.ModuleID)));
            }
            catch (Exception exception4)
            {
                ProjectData.SetProjectError(exception4);
                
            }
            try
            {
                objModuleInfo.ModuleDefID = Convert.ToInt32(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["ModuleDefID"]), objModuleInfo.ModuleDefID)));
            }
            catch (Exception exception5)
            {
                ProjectData.SetProjectError(exception5);
                
            }
            try
            {
                objModuleInfo.ModuleOrder = Convert.ToInt32(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["ModuleOrder"]), objModuleInfo.ModuleOrder)));
            }
            catch (Exception exception6)
            {
                ProjectData.SetProjectError(exception6);
                
            }
            try
            {
                objModuleInfo.PaneName = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["PaneName"]), objModuleInfo.PaneName)));
            }
            catch (Exception exception7)
            {
                ProjectData.SetProjectError(exception7);
                
            }
            try
            {
                objModuleInfo.ModuleTitle = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["ModuleTitle"]), objModuleInfo.ModuleTitle)));
            }
            catch (Exception exception8)
            {
                ProjectData.SetProjectError(exception8);
                
            }
            try
            {
                objModuleInfo.CacheTime = Convert.ToInt32(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["CacheTime"]), objModuleInfo.CacheTime)));
            }
            catch (Exception exception9)
            {
                ProjectData.SetProjectError(exception9);
                
            }
            try
            {
                objModuleInfo.Alignment = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["Alignment"]), objModuleInfo.Alignment)));
            }
            catch (Exception exception10)
            {
                ProjectData.SetProjectError(exception10);
                
            }
            try
            {
                objModuleInfo.Color = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["Color"]), objModuleInfo.Color)));
            }
            catch (Exception exception11)
            {
                ProjectData.SetProjectError(exception11);
                
            }
            try
            {
                objModuleInfo.Border = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["Border"]), objModuleInfo.Border)));
            }
            catch (Exception exception12)
            {
                ProjectData.SetProjectError(exception12);
                
            }
            try
            {
                objModuleInfo.IconFile = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["IconFile"]), objModuleInfo.IconFile)));
            }
            catch (Exception exception13)
            {
                ProjectData.SetProjectError(exception13);
                
            }
            try
            {
                objModuleInfo.AllTabs = Convert.ToBoolean(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["AllTabs"]), objModuleInfo.AllTabs)));
            }
            catch (Exception exception14)
            {
                ProjectData.SetProjectError(exception14);
                
            }
            try
            {
                int intVisibility=0;
                int refIntHelperL0 = Convert.ToInt32(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["Visibility"]), intVisibility)));
                if ((((refIntHelperL0 == 0) || (refIntHelperL0 == Null.NullInteger)) ? 1 : 0) != 0)
                {
                    objModuleInfo.Visibility = VisibilityState.Maximized;
                }
                else if (refIntHelperL0 == 1)
                {
                    objModuleInfo.Visibility = VisibilityState.Minimized;
                }
                else if (refIntHelperL0 == 2)
                {
                    objModuleInfo.Visibility = VisibilityState.None;
                }
            }
            catch (Exception exception15)
            {
                ProjectData.SetProjectError(exception15);
                
            }
            try
            {
                objModuleInfo.IsDeleted = Convert.ToBoolean(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["IsDeleted"]), objModuleInfo.IsDeleted)));
            }
            catch (Exception exception16)
            {
                ProjectData.SetProjectError(exception16);
                
            }
            try
            {
                objModuleInfo.Header = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["Header"]), objModuleInfo.Header)));
            }
            catch (Exception exception17)
            {
                ProjectData.SetProjectError(exception17);
                
            }
            try
            {
                objModuleInfo.Footer = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["Footer"]), objModuleInfo.Footer)));
            }
            catch (Exception exception18)
            {
                ProjectData.SetProjectError(exception18);
                
            }
            try
            {
                objModuleInfo.StartDate = Convert.ToDateTime(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["StartDate"]), objModuleInfo.StartDate)));
            }
            catch (Exception exception19)
            {
                ProjectData.SetProjectError(exception19);
                
            }
            try
            {
                objModuleInfo.EndDate = Convert.ToDateTime(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["EndDate"]), objModuleInfo.EndDate)));
            }
            catch (Exception exception20)
            {
                ProjectData.SetProjectError(exception20);
                
            }
            try
            {
                objModuleInfo.ContainerSrc = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["ContainerSrc"]), objModuleInfo.ContainerSrc)));
            }
            catch (Exception exception21)
            {
                ProjectData.SetProjectError(exception21);
                
            }
            try
            {
                objModuleInfo.DisplayTitle = Convert.ToBoolean(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["DisplayTitle"]), objModuleInfo.DisplayTitle)));
            }
            catch (Exception exception22)
            {
                ProjectData.SetProjectError(exception22);
                
            }
            try
            {
                objModuleInfo.DisplayPrint = Convert.ToBoolean(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["DisplayPrint"]), objModuleInfo.DisplayPrint)));
            }
            catch (Exception exception23)
            {
                ProjectData.SetProjectError(exception23);
                
            }
            try
            {
                objModuleInfo.DisplaySyndicate = Convert.ToBoolean(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["DisplaySyndicate"]), objModuleInfo.DisplaySyndicate)));
            }
            catch (Exception exception24)
            {
                ProjectData.SetProjectError(exception24);
                
            }
            try
            {
                objModuleInfo.InheritViewPermissions = Convert.ToBoolean(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["InheritViewPermissions"]), objModuleInfo.InheritViewPermissions)));
            }
            catch (Exception exception25)
            {
                ProjectData.SetProjectError(exception25);
                
            }
            try
            {
                objModuleInfo.DesktopModuleID = Convert.ToInt32(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["DesktopModuleID"]), objModuleInfo.DesktopModuleID)));
            }
            catch (Exception exception26)
            {
                ProjectData.SetProjectError(exception26);
                
            }
            try
            {
                objModuleInfo.FriendlyName = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["FriendlyName"]), objModuleInfo.FriendlyName)));
            }
            catch (Exception exception27)
            {
                ProjectData.SetProjectError(exception27);
                
            }
            try
            {
                objModuleInfo.Description = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["Description"]), objModuleInfo.Description)));
            }
            catch (Exception exception28)
            {
                ProjectData.SetProjectError(exception28);
                
            }
            try
            {
                objModuleInfo.Version = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["Version"]), objModuleInfo.Version)));
            }
            catch (Exception exception29)
            {
                ProjectData.SetProjectError(exception29);
                
            }
            try
            {
                objModuleInfo.IsPremium = Convert.ToBoolean(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["IsPremium"]), objModuleInfo.IsPremium)));
            }
            catch (Exception exception30)
            {
                ProjectData.SetProjectError(exception30);
                
            }
            try
            {
                objModuleInfo.IsAdmin = Convert.ToBoolean(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["IsAdmin"]), objModuleInfo.IsAdmin)));
            }
            catch (Exception exception31)
            {
                ProjectData.SetProjectError(exception31);
                
            }
            try
            {
                objModuleInfo.BusinessControllerClass = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["BusinessControllerClass"]), objModuleInfo.BusinessControllerClass)));
            }
            catch (Exception exception32)
            {
                ProjectData.SetProjectError(exception32);
                
            }
            try
            {
                objModuleInfo.SupportedFeatures = Convert.ToInt32(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["SupportedFeatures"]), objModuleInfo.SupportedFeatures)));
            }
            catch (Exception exception33)
            {
                ProjectData.SetProjectError(exception33);
                
            }
            try
            {
                objModuleInfo.ModuleControlId = Convert.ToInt32(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["ModuleControlId"]), objModuleInfo.ModuleControlId)));
            }
            catch (Exception exception34)
            {
                ProjectData.SetProjectError(exception34);
                
            }
            try
            {
                objModuleInfo.ControlSrc = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["ControlSrc"]), objModuleInfo.ControlSrc)));
            }
            catch (Exception exception35)
            {
                ProjectData.SetProjectError(exception35);
                
            }
            try
            {
                int intControlType=0;
                int refIntHelperL1 = Convert.ToInt32(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["ControlType"]), intControlType)));
                switch (refIntHelperL1)
                {
                    case -3:
                        objModuleInfo.ControlType = SecurityAccessLevel.ControlPanel;
                        goto Label_0A1E;

                    case -2:
                        objModuleInfo.ControlType = SecurityAccessLevel.SkinObject;
                        goto Label_0A1E;
                }
                if ((((refIntHelperL1 == -1) || (refIntHelperL1 == Null.NullInteger)) ? 1 : 0) != 0)
                {
                    objModuleInfo.ControlType = SecurityAccessLevel.Anonymous;
                }
                else if (refIntHelperL1 == 0)
                {
                    objModuleInfo.ControlType = SecurityAccessLevel.View;
                }
                else if (refIntHelperL1 == 1)
                {
                    objModuleInfo.ControlType = SecurityAccessLevel.Edit;
                }
                else if (refIntHelperL1 == 2)
                {
                    objModuleInfo.ControlType = SecurityAccessLevel.Admin;
                }
                else if (refIntHelperL1 == 3)
                {
                    objModuleInfo.ControlType = SecurityAccessLevel.Host;
                }
            }
            catch (Exception exception36)
            {
                ProjectData.SetProjectError(exception36);
                
            }
        Label_0A1E:;
            try
            {
                objModuleInfo.ControlTitle = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["ControlTitle"]), objModuleInfo.ControlTitle)));
            }
            catch (Exception exception37)
            {
                ProjectData.SetProjectError(exception37);
                
            }
            try
            {
                objModuleInfo.HelpUrl = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["HelpUrl"]), objModuleInfo.HelpUrl)));
            }
            catch (Exception exception38)
            {
                ProjectData.SetProjectError(exception38);
                
            }
            if (IncludePermissions)
            {
                if (objModuleInfo == null)
                {
                    return objModuleInfo;
                }
                try
                {
                    objModuleInfo.ModulePermissions = objModulePermissionController.GetModulePermissionsCollectionByModuleID(objModuleInfo.ModuleID);
                }
                catch (Exception exception39)
                {
                    ProjectData.SetProjectError(exception39);
                    
                }
                try
                {
                    objModuleInfo.AuthorizedEditRoles = objModulePermissionController.GetModulePermissionsByModuleID(objModuleInfo, "EDIT");
                    if (objModuleInfo.AuthorizedEditRoles == ";")
                    {
                        try
                        {
                            objModuleInfo.AuthorizedEditRoles = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["AuthorizedEditRoles"]), objModuleInfo.AuthorizedEditRoles)));
                        }
                        catch (Exception exception40)
                        {
                            ProjectData.SetProjectError(exception40);
                            
                        }
                    }
                }
                catch (Exception exception41)
                {
                    ProjectData.SetProjectError(exception41);
                    
                }
                try
                {
                    if (objModuleInfo.InheritViewPermissions)
                    {
                        TabPermissionController objTabPermissionController = new TabPermissionController();
                        ArrayList arrTabPermissions = objTabPermissionController.GetTabPermissionsByPortal(objModuleInfo.PortalID);
                        objModuleInfo.AuthorizedViewRoles = objTabPermissionController.GetTabPermissionsByTabID(arrTabPermissions, objModuleInfo.TabID, "VIEW");
                    }
                    else
                    {
                        objModuleInfo.AuthorizedViewRoles = objModulePermissionController.GetModulePermissionsByModuleID(objModuleInfo, "VIEW");
                    }
                    if (objModuleInfo.AuthorizedViewRoles == ";")
                    {
                        try
                        {
                            objModuleInfo.AuthorizedViewRoles = Convert.ToString(RuntimeHelpers.GetObjectValue(Null.SetNull(RuntimeHelpers.GetObjectValue(dr["AuthorizedViewRoles"]), objModuleInfo.AuthorizedViewRoles)));
                        }
                        catch (Exception exception42)
                        {
                            ProjectData.SetProjectError(exception42);
                            
                        }
                    }
                    return objModuleInfo;
                }
                catch (Exception exception43)
                {
                    ProjectData.SetProjectError(exception43);
                    
                }
            }
            return objModuleInfo;
        }

        internal ArrayList FillModuleInfoCollection(IDataReader dr)
        {
            return this.FillModuleInfoCollection(dr, true);
        }

        internal ArrayList FillModuleInfoCollection(IDataReader dr, bool IncludePermissions)
        {
            ArrayList arr = new ArrayList();
            try
            {
                while (dr.Read())
                {
                    ModuleInfo obj = this.FillModuleInfo(dr, false, IncludePermissions);
                    arr.Add(obj);
                }
            }
            catch (Exception exception1)
            {
                
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.LogException(exc);
                
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
            }
            return arr;
        }

        public ArrayList GetAllModules()
        {
            return this.FillModuleInfoCollection(DataProvider.Instance().GetAllModules());
        }

        public ArrayList GetAllTabsModules(int PortalID, bool AllTabs)
        {
            return this.FillModuleInfoCollection(DataProvider.Instance().GetAllTabsModules(PortalID, AllTabs));
        }

        public ModuleInfo GetModule(int ModuleId, int TabId)
        {
            ModuleInfo GetModule;
            IDataReader dr = DataProvider.Instance().GetModule(ModuleId, TabId);
            try
            {
                GetModule = this.FillModuleInfo(dr);
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
            }
            return GetModule;
        }

        public ModuleInfo GetModuleByDefinition(int PortalId, string FriendlyName)
        {
            ModuleInfo GetModuleByDefinition;
            IDataReader dr = DataProvider.Instance().GetModuleByDefinition(PortalId, FriendlyName);
            try
            {
                GetModuleByDefinition = this.FillModuleInfo(dr);
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
            }
            return GetModuleByDefinition;
        }

        public ArrayList GetModules(int PortalID)
        {
            return this.FillModuleInfoCollection(DataProvider.Instance().GetModules(PortalID));
        }

        public ArrayList GetModules(int PortalID, bool IncludePermissions)
        {
            return this.FillModuleInfoCollection(DataProvider.Instance().GetModules(PortalID), IncludePermissions);
        }

        public ArrayList GetModulesByDefinition(int PortalId, string FriendlyName)
        {
            return this.FillModuleInfoCollection(DataProvider.Instance().GetModuleByDefinition(PortalId, FriendlyName));
        }

        public Hashtable GetModuleSettings(int ModuleId)
        {
            Hashtable objModuleSettings = new Hashtable();
            IDataReader dr = DataProvider.Instance().GetModuleSettings(ModuleId);
            while (dr.Read())
            {
                if (!dr.IsDBNull(1))
                {
                    objModuleSettings[dr.GetString(0)] = dr.GetString(1);
                }
                else
                {
                    objModuleSettings[dr.GetString(0)] = "";
                }
            }
            dr.Close();
            return objModuleSettings;
        }

        public ArrayList GetPortalTabModules(int PortalId, int TabId)
        {
            return this.FillModuleInfoCollection(DataProvider.Instance().GetPortalTabModules(PortalId, TabId));
        }

        public ArrayList GetSearchModules(int PortalId)
        {
            return this.FillModuleInfoCollection(DataProvider.Instance().GetSearchModules(PortalId));
        }

        public Hashtable GetTabModuleSettings(int TabModuleId)
        {
            Hashtable objTabModuleSettings = new Hashtable();
            IDataReader dr = DataProvider.Instance().GetTabModuleSettings(TabModuleId);
            while (dr.Read())
            {
                if (!dr.IsDBNull(1))
                {
                    objTabModuleSettings[dr.GetString(0)] = dr.GetString(1);
                }
                else
                {
                    objTabModuleSettings[dr.GetString(0)] = "";
                }
            }
            dr.Close();
            return objTabModuleSettings;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// MoveModule moes a Module from one Tab to another including all the 
        /// TabModule settings
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="moduleId">The Id of the module to move</param>
        /// <param name="fromTabId">The Id of the source tab</param>
        /// <param name="toTabId">The Id of the destination tab</param>
        /// <param name="toPaneName">The name of the Pane on the destination tab where the module will end up</param>
        /// <history>
        /// [cnurse]	10/21/2004	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public void MoveModule(int moduleId, int fromTabId, int toTabId, string toPaneName)
        {
            this.CopyModule(moduleId, fromTabId, toTabId, toPaneName, true);
            this.DeleteTabModule(fromTabId, moduleId);
        }

        public void UpdateModule(ModuleInfo objModule)
        {
            DataProvider.Instance().UpdateModule(objModule.ModuleID, objModule.ModuleTitle, objModule.AllTabs, objModule.Header, objModule.Footer, objModule.StartDate, objModule.EndDate, objModule.InheritViewPermissions, objModule.IsDeleted);
            ModulePermissionController objModulePermissionController = new ModulePermissionController();
            if (!objModulePermissionController.GetModulePermissionsCollectionByModuleID(objModule.ModuleID).CompareTo(objModule.ModulePermissions))
            {
                IEnumerator refObjectHelperL0=null;
                objModulePermissionController.DeleteModulePermissionsByModuleID(objModule.ModuleID);
                try
                {
                    refObjectHelperL0 = objModule.ModulePermissions.GetEnumerator();
                    while (refObjectHelperL0.MoveNext())
                    {
                        ModulePermissionInfo objModulePermission = (ModulePermissionInfo) refObjectHelperL0.Current;
                        objModulePermission.ModuleID = objModule.ModuleID;
                        if (objModule.InheritViewPermissions & (objModulePermission.PermissionKey == "VIEW"))
                        {
                            objModulePermissionController.DeleteModulePermission(objModulePermission.ModulePermissionID);
                        }
                        else if (objModulePermission.AllowAccess)
                        {
                            objModulePermissionController.AddModulePermission(objModulePermission);
                        }
                    }
                }
                finally
                {
                    if (refObjectHelperL0 is IDisposable)
                    {
                        (refObjectHelperL0 as IDisposable).Dispose();
                    }
                }
            }
            if (!Null.IsNull(objModule.TabID))
            {
                DataProvider.Instance().UpdateTabModule(objModule.TabID, objModule.ModuleID, objModule.ModuleOrder, objModule.PaneName, objModule.CacheTime, objModule.Alignment, objModule.Color, objModule.Border, objModule.IconFile, (int) objModule.Visibility, objModule.ContainerSrc, objModule.DisplayTitle, objModule.DisplayPrint, objModule.DisplaySyndicate);
                this.UpdateModuleOrder(objModule.TabID, objModule.ModuleID, objModule.ModuleOrder, objModule.PaneName);
                if (objModule.IsDefaultModule)
                {
                    PortalSettings.UpdatePortalSetting(objModule.PortalID, "defaultmoduleid", objModule.ModuleID.ToString());
                    PortalSettings.UpdatePortalSetting(objModule.PortalID, "defaulttabid", objModule.TabID.ToString());
                }
                if (objModule.AllModules)
                {
                    IEnumerator refObjectHelperL1=null;
                    ArrayList arrTabs = new TabController().GetTabs(objModule.PortalID);
                    try
                    {
                        refObjectHelperL1 = arrTabs.GetEnumerator();
                        while (refObjectHelperL1.MoveNext())
                        {
                            TabInfo objTab = (TabInfo) refObjectHelperL1.Current;
                            if (!objTab.IsAdminTab)
                            {
                                IEnumerator refObjectHelperL2=null;
                                ArrayList arrModules = this.GetPortalTabModules(objTab.PortalID, objTab.TabID);
                                try
                                {
                                    refObjectHelperL2 = arrModules.GetEnumerator();
                                    while (refObjectHelperL2.MoveNext())
                                    {
                                        ModuleInfo objTargetModule = (ModuleInfo) refObjectHelperL2.Current;
                                        DataProvider.Instance().UpdateTabModule(objTargetModule.TabID, objTargetModule.ModuleID, objTargetModule.ModuleOrder, objTargetModule.PaneName, objModule.CacheTime, objModule.Alignment, objModule.Color, objModule.Border, objModule.IconFile, (int) objModule.Visibility, objModule.ContainerSrc, objModule.DisplayTitle, objModule.DisplayPrint, objModule.DisplaySyndicate);
                                    }
                                }
                                finally
                                {
                                    if (refObjectHelperL2 is IDisposable)
                                    {
                                        (refObjectHelperL2 as IDisposable).Dispose();
                                    }
                                }
                            }
                        }
                    }
                    finally
                    {
                        if (refObjectHelperL1 is IDisposable)
                        {
                            (refObjectHelperL1 as IDisposable).Dispose();
                        }
                    }
                }
            }
        }

        public void UpdateModuleOrder(int TabId, int ModuleId, int ModuleOrder, string PaneName)
        {
            ModuleInfo objModule = this.GetModule(ModuleId, TabId);
            if (objModule != null)
            {
                if (ModuleOrder == -1)
                {
                    IDataReader dr = DataProvider.Instance().GetTabModuleOrder(TabId, PaneName);
                    while (dr.Read())
                    {
                        ModuleOrder = Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr["ModuleOrder"]));
                    }
                    dr.Close();
                    ModuleOrder += 2;
                }
                DataProvider.Instance().UpdateModuleOrder(TabId, ModuleId, ModuleOrder, PaneName);
                if (!objModule.AllTabs)
                {
                    DataCache.ClearTabCache(TabId, objModule.PortalID);
                }
                else
                {
                    TabInfo objTab = new TabController().GetTab(TabId);
                    if (objTab != null)
                    {
                        DataCache.ClearPortalCache(objTab.PortalID, true);
                    }
                }
            }
        }

        public void UpdateModuleSetting(int ModuleId, string SettingName, string SettingValue)
        {
            IDataReader dr = DataProvider.Instance().GetModuleSetting(ModuleId, SettingName);
            if (dr.Read())
            {
                DataProvider.Instance().UpdateModuleSetting(ModuleId, SettingName, SettingValue);
            }
            else
            {
                DataProvider.Instance().AddModuleSetting(ModuleId, SettingName, SettingValue);
            }
            dr.Close();
        }

        [Obsolete("Use the new UpdateTabModuleOrder(tabid,portalid)")]
        public void UpdateTabModuleOrder(int TabId)
        {
            IDataReader dr = DataProvider.Instance().GetTabPanes(TabId);
            while (dr.Read())
            {
                int ModuleCounter = 0;
                IDataReader dr2 = DataProvider.Instance().GetTabModuleOrder(TabId, Convert.ToString(RuntimeHelpers.GetObjectValue(dr["PaneName"])));
                while (dr2.Read())
                {
                    ModuleCounter++;
                    DataProvider.Instance().UpdateModuleOrder(TabId, Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr2["ModuleID"])), (ModuleCounter * 2) - 1, Convert.ToString(RuntimeHelpers.GetObjectValue(dr["PaneName"])));
                }
                dr2.Close();
            }
            dr.Close();
            TabInfo objTab = new TabController().GetTab(TabId);
            DataCache.ClearTabCache(TabId, objTab.PortalID);
        }

        public void UpdateTabModuleOrder(int TabId, int PortalId)
        {
            IDataReader dr = DataProvider.Instance().GetTabPanes(TabId);
            while (dr.Read())
            {
                int ModuleCounter = 0;
                IDataReader dr2 = DataProvider.Instance().GetTabModuleOrder(TabId, Convert.ToString(RuntimeHelpers.GetObjectValue(dr["PaneName"])));
                while (dr2.Read())
                {
                    ModuleCounter++;
                    DataProvider.Instance().UpdateModuleOrder(TabId, Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr2["ModuleID"])), (ModuleCounter * 2) - 1, Convert.ToString(RuntimeHelpers.GetObjectValue(dr["PaneName"])));
                }
                dr2.Close();
            }
            dr.Close();
            DataCache.ClearTabCache(TabId, PortalId);
        }

        public void UpdateTabModuleSetting(int TabModuleId, string SettingName, string SettingValue)
        {
            IDataReader dr = DataProvider.Instance().GetTabModuleSetting(TabModuleId, SettingName);
            if (dr.Read())
            {
                DataProvider.Instance().UpdateTabModuleSetting(TabModuleId, SettingName, SettingValue);
            }
            else
            {
                DataProvider.Instance().AddTabModuleSetting(TabModuleId, SettingName, SettingValue);
            }
            dr.Close();
        }
    }
}

